package cn.cloudreal.cbms.material.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.material.dao.MaterialDao;
import cn.cloudreal.cbms.system.dao.impl.SampleDaoImpl;
import cn.cloudreal.cbms.vo.Material;

public class MaterialDaoImpl extends SqlMapClientDaoSupport implements MaterialDao
{
    private final String GET_ALL_Material = "material.getAllList";
    private final String GET_ALL_Material_COUNT = "material.countMaterial";
    private final String ADD_Material="material.intsertMaterial";
    private final String UPDATE_Material="material.updateOfMaterial";
    private final String DELETE_Material="material.deleteOfMaterial";
    private final String DETAIL_Material="material.detailOfMaterial";
    private final String JUDGE_MaterialId="material.uniqueOfMaterialId";
    private final String DELETE_Content="material.deleteOfMaterialContent";
    private final String SELECT_ID_PM="material.selectIdOfPM";
    private final String SELECT_Content="material.selectOfContent";
    
    

    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(SampleDaoImpl.class);

    /*
     * 查询物料库的所有列表
     */
    public List<Material> getMaterialList(PageBean page, Material material)
    {
    	List<Material> list = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter MaterialDaoImpl getMaterialList() method");
        }
        try {
        	// 分页功能,计算总量
        	int totalCount = (Integer) getSqlMapClientTemplate().queryForObject(GET_ALL_Material_COUNT,
        			material);
        	page.setTotalRecords(totalCount);
        	if (totalCount == 0)
        	{
        		return null;
        	}
        	Map<String, Object> param = new HashMap<String, Object>();
        	
        	param.put("begin", page.getPageSize() * (page.getPageNo() - 1) + 1);
        	param.put("end", page.getPageSize() * page.getPageNo());
        	
        	param.put("firstUpTime", material.getFirstUpTime()); // 开始时间
        	param.put("documentDate", material.getDocumentDate()); // 结束时间
        	
        	param.put("materialType", material.getMaterialType()); // 物料类型
        	param.put("ownCity", material.getOwnCity()); // 归属地市
        	
        	param.put("materialID", material.getMaterialID()); // 物料编号
        	param.put("serviceType", material.getServiceType());// 项目名称
        	list = this.getSqlMapClientTemplate().queryForList(GET_ALL_Material, param);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MaterialDaoImpl getMaterialList() method");
        }
        return list;
    }

    /**
     * 新增物料信息
     * @return 
     */
    public Integer addMaterial(Material material)
    {

        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter MaterialDaoImpl addMaterial() method");
        }

        Integer addId = (Integer) getSqlMapClientTemplate().insert(ADD_Material, material);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MaterialDaoImpl addMaterial() method");
        }
		return addId;

    }

    /**
     * 修改物料信息
     */

    public void updateMaterial(Material material)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter MaterialDaoImpl updateMaterial() method");
        }

        getSqlMapClientTemplate().update(UPDATE_Material, material);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MaterialDaoImpl updateMaterial() method");
        }

    }

    /**
     * 删除物料信息
     */
    public void deleteMaterial(Integer matId)
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter MaterialDaoImpl deleteMaterial() method");
        }

        getSqlMapClientTemplate().delete(DELETE_Material, matId);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MaterialDaoImpl deleteMaterial() method");
        }

    }

    /**
	 * 根据matId查询物料信息
	 */
    public Material detailMaterial(Integer matId)
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter MaterialDaoImpl deleteMaterial() method");
        }
        Material material;
        material = (Material) getSqlMapClientTemplate().queryForObject(DETAIL_Material,
                matId);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MaterialDaoImpl deleteMaterial() method");
        }
        return material;
    }

    /**
     * 查看物料编号是否唯一
     */
    public boolean isTheUniqueId(String materialId)
    {
        if (log.isInfoEnabled())
        {
            log.info("Enter MaterialDaoImpl isTheUniqueId() method");
        }
        List<Material> list = getSqlMapClientTemplate().queryForList(JUDGE_MaterialId,
                materialId);
        if (list.size() > 0)
        {
            return false;
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MaterialDaoImpl isTheUniqueId() method");
        }
        return true;
    }

    /**
     * 删除物料文件
     */
	public void deleteContent(String materialContent) 
	{
		if (log.isInfoEnabled())
        {
            log.info("Enter MaterialDaoImpl deleteContent() method");
        }
        getSqlMapClientTemplate().update(DELETE_Content, materialContent);
     
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MaterialDaoImpl deleteContent() method");
        }
		
	}

	/**
     * 模糊查询项目媒介表中是否存在物料的自增id
     */
	public boolean isExitIDInPM(Integer matId) 
	{
		if (log.isInfoEnabled())
        {
            log.info("Enter MaterialDaoImpl isExitIDInPM() method");
        }
		List<Integer> proMedId;
		proMedId = getSqlMapClientTemplate().queryForList(SELECT_ID_PM,matId);
		if(proMedId.size()>0)return false;
		if (log.isInfoEnabled())
        {
            log.info("Exit MaterialDaoImpl isExitIDInPM() method");
        }
		return true;
	}

	/**
	 * 查询改物料文件名是否存在
	 */
	public boolean isExitContent(String materialContent) {
		if (log.isInfoEnabled())
        {
            log.info("Enter MaterialDaoImpl isExitContent() method");
        }
		
		String contentName = (String) getSqlMapClientTemplate().queryForObject(SELECT_Content,materialContent);
		if(contentName!=null&&contentName!="")return true;
		
		if (log.isInfoEnabled())
        {
            log.info("Exit MaterialDaoImpl isExitContent() method");
        }
		return false;
	}

	

}
